<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class RequestDataUtil
{
	
	function toInt(&$arr, $keys) {
		foreach($keys as $k => $v) {
			if(array_key_exists($v, $arr)) {
				$arr[$v] = (int) $arr[$v];
			}
		}
		
		return $arr;
	}
	
	
/*
	function toSafe(&$arr, $keys) {
		foreach($keys as $k => $v) {
			if(array_key_exists($v, $arr)) {
				$arr[$v] = (int) $arr[$v];
			}
		}
		
		return $arr;
	}	
*/


	function _stripVarsOut($search, $replace, $string) {
		return str_replace($search, $replace, $string);
	}
	
	
	function _getRulesOut() {
		$a['search']  = array('&',    '"',     "'",      '>',    '<');
		$a['replace'] = array('&amp', '&quot', '&#039;', '&gt;', '&lt');
		return $a;
	}
	
/*
'&' (ampersand) becomes '&amp;' 
'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set. 
''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. 
'<' (less than) becomes '&lt;' 
'>' (greater than) becomes '&gt;' 
*/
	
	function &stripVarsOut(&$arr, $skip_keys = array(), $wordrap_num = 75) {
		
		foreach($arr as $k => $v){
			if(is_array($v)) {
				$arr[$k] = RequestDataUtil::stripVarsOut($v, $skip_keys, $wordrap_num);
			} else {
				if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }
				$arr[$k] = wordwrap(addslashes(stripslashes(trim(htmlspecialchars($v, ENT_NOQUOTES)))), $wordrap_num, ' ', 1);
			}
		}
		return $arr;
	}
	
	
	
	// recursive
	// working with $_POST or $_GET variables change < to &lt; and so on
	// to be able to show the same after submit and error
	// by default $server_check = false, if true than we check errors on server side 
	// and after check before insert in db need to call - stripVars(&$arr, false);
	function & stripVars(&$arr, $skip_keys = array(), $server_check = false, $wordrap_num = 75) {
		
		foreach($arr as $k => $v){
			if(is_array($v)) {
				$arr[$k] = RequestDataUtil::stripVars($v, $skip_keys, $server_check, $wordrap_num);
			} else {
				if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }
				$arr[$k] = wordwrap(stripslashes(trim(htmlspecialchars($v, ENT_NOQUOTES))), $wordrap_num, ' ', 1);
				if(empty($server_check)) { $arr[$k] = addslashes($arr[$k]); }
			}
		}
		return $arr;
	}
	
	
	function & stripVarsHtml(&$arr, $skip_keys = array(),  $server_check = false) {
		
		foreach($arr as $k => $v){
			if(is_array($v)) {
				$arr[$k] = RequestDataUtil::stripVarsHtml($v, $skip_keys, $server_check);
			} else {
				if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }
				$arr[$k] = stripslashes($v);
				if(empty($server_check)) { $arr[$k] = addslashes($arr[$k]); }
			}
		}
		
		return $arr;
	}
}

?>